<div class="row">
    <div class="col-sm-6">
        <div class="panel panel-primary">
            <div class="panel-heading">
                <h3 class="panel-title">Filtro</h3>
            </div>
            <div class="panel-body">
                <form class="form-inline" method="POST" role="form">
                    <div class="form-group">
                        <label class="sr-only" for="txtLegajoIdPedido">Legajo o Id Pedido</label>
                        <input type="text" class="form-control" id="txtLegajoIdPedido" name="txtLegajoIdPedido" placeholder="Legajo o Id Pedido" required="required">
                    </div>
                    <button id="btnFiltrarPedido" type="submit" class="btn btn-default">
                        <span class="glyphicon glyphicon-filter"></span> Filtrar
                    </button>
                </form>
            </div>
        </div>
    </div>
    <div class="col-sm-6">
        <div class="panel panel-primary">
            <div class="panel-heading">
                <h3 class="panel-title">Verificar estado</h3>
            </div>
            <div class="panel-body">
                <form id="frmVerificarPedido" class="form-inline" role="form">
                    <div class="form-group">
                        <label class="sr-only" for="txtEstadoIDPedido">ID Pedido</label>
                        <input type="text" class="form-control" id="txtEstadoIDPedido" name="txtEstadoIDPedido" placeholder="ID Pedido" required="required">
                    </div>
                    <div class="form-group">
                        <button id="btnVerEstadoPedido" type="submit" class="btn btn-default">
                            <span class="glyphicon glyphicon-search"></span> Verificar
                        </button>
                    </div>
                    <div class="form-group">
                        <strong>Estado:</strong> <span id="sEstadoIDPedido"></span>
                    </div>
                    <div class="form-group">
                        <strong>Pagado:</strong> <span id="sPagadoIDPedido"></span>
                    </div>
                </form>
            </div>
        </div>
    </div>
</div>
<div class="panel panel-primary">
    <div class="panel-heading">
        <div class="row">
            <div class="col-md-8">
                <h3 class="panel-title">Resultados</h3>
            </div>
            <div class="col-md-2">
                <button id="btnLimpiar" class="btn btn-info btn-block" type="button">
                    <span class="glyphicon glyphicon-unchecked"></span> Limpiar
                </button>
            </div>
            <div class="col-md-2">
                <button id="btnActualizar" class="btn btn-info btn-block" type="button">
                    <span class="glyphicon glyphicon-refresh"></span> Actualizar
                </button>
            </div>
        </div>
    </div>
    <div class="panel-body">
        <div class="table-responsive">
            <table class="table table-hover">
                <thead>
                    <tr>
                        <th>Id</th>
                        <th>Legajo</th>
                        <th>Apellido</th>
                        <!-- <th>Descripcion</th> -->
                        <th>Posicion</th>
                        <th>Fecha</th>
                        <th>Franja</th>
                        <th>&iquest;Entregado?</th>
                    </tr>
                </thead>
                <tbody>
                    {table_content}
                </tbody>
            </table>
        </div>
    </div>
</div>
<script type="text/javascript">
    $("#btnLimpiar").click(function() {
        $("input[type=checkbox]:checked").each(function(index, item) {
            $(item).trigger("click");
        });
    });

    $("#btnActualizar").click(function() {
        location.reload();
    });

    $("#frmVerificarPedido").submit(function(e) {
        e.preventDefault();
        if($("#txtEstadoIDPedido").val().length > 0) {
            $.ajax({
                url: "index.php?do=/atpublico/ajax_pedido_estado",
                type: "POST",
                data: {idPedido: $("#txtEstadoIDPedido").val()},
                success: function(data, textStatus, jqXHR) {
                    var ret = JSON.parse(data);
                    console.debug(ret[0]);
                    $("#sEstadoIDPedido").text(ret[0].Descripcion);
                    $("#sPagadoIDPedido").text(ret[0].Pagado);
                },
                error: function(jqXHR, textStatus, errorThrown) {
                    console.error(jqXHR, textStatus, errorThrown);
                }
            });
        };
    });

    function postChangeItemStatus(item, value) {
        $.post(
                "index.php?do=/atpublico/ajax_detail_item_change_status",
                {
                    idPedido: item,
                    toValue: value
                }
        ).fail(function() {
            alert("error");
        });
    }

    $(".chkEntregado").change(function() {
        if (this.checked) {
            postChangeItemStatus(this.value, true);
        } else {
            postChangeItemStatus(this.value, false);
        }
    });
</script>